<?php

class branch_IndexController extends Zend_Controller_Action
{
    public function init()
    {
    	defined('BASE_URL')	|| define('BASE_URL', Zend_Controller_Front::getInstance()->getBaseUrl());
    }

    public function indexAction()
    {
    	
    	if($this->getRequest()->isPost()){
    		$data = $this->getRequest()->getPost();
    		if(isset($data['status'])){
    			$ids = $data['status'];
    			$db->updateUnStatus($ids);
    			$this->_redirect("/users/");   			
    		}elseif (isset($data['unstatus'])){
    			$ids = $data['unstatus'];
    			$db->updateStatus($ids);
    			$this->_redirect("/users/");
    		}   		
    		$id = $data['checkBox']; 
    		if(isset($data['update'])&& $id !=""){   			
    			$this->_redirect("/users/index/edit/id/".$id);
    			$row = $db->getUserType($id);
    			$frmuser = new users_Form_FrmUserType();
    			$this->view->frmusers = $frmuser->frmTypeUser($row);
    		}elseif(isset($data['delete'])&& $id !=""){
		    	$db->deleteUser($id);
		    	Application_Form_FrmMessage::message("áž€áž¶áž›áž»áž”áž”áŸ’ážšáž—áŸ�áž‘áž¢áŸ’áž“áž€áž”áŸ’ážšáž¾áž”áŸ’ážšáž¶ážŸáŸ‹áž”áž¶áž“áž‡áŸ„áž‚áž‡áŸ�áž™");
		    	Application_Form_FrmMessage::redirectUrl('/users/');
    		}else { $this->_redirect("/users/");}
    		
    	}
    	$db = new branch_Model_DbTable_DbBranch();
    	$datas = $db->getAllBranch();
    	$this->view->getdata = $datas;
    	
	}
	public function addBranchAction(){
		if($this->getRequest()->isPost()){
			$data = $this->getRequest()->getPost();
			$db = new branch_Model_DbTable_DbBranch();
			$db->addBranch($data);
			if(isset($data['save'])){
				Application_Form_FrmMessage::message("បញ្ចូលជោគជ័យ!");
			}
			if(isset($data['save_close'])){
				Application_Form_FrmMessage::message("បញ្ចូលជោគជ័យ");
				Application_Form_FrmMessage::redirectUrl('/branch/index');
			}
		}
		$form = new branch_Form_FrmBranch();
		$this->view->frmBranch = $form->frmBranch();
	}
	public function editBranchAction()
	{
		$id = $this->getRequest()->getParam('id');
		if($this->getRequest()->isPost()){
			$data = $this->getRequest()->getPost();
			$update = new branch_Model_DbTable_DbBranch();
			$update->editBranch($data);
			if(isset($data['save'])){
				Application_Form_FrmMessage::message("កែប្រែជោគជ័យ");
				Application_Form_FrmMessage::redirectUrl('/branch/index/edit-branch/id/'.$id);
			}
			if(isset($data['save_add'])){
				Application_Form_FrmMessage::message("កែប្រែជោគជ័យ");
				Application_Form_FrmMessage::redirectUrl('/branch/add-branch');
			}
			if(isset($data['save_close'])){
				Application_Form_FrmMessage::message("កែប្រែជោគជ័យ");
				Application_Form_FrmMessage::redirectUrl('/branch/index');
			}
		}
		$db = new branch_Model_DbTable_DbBranch();
		$row = $db->getBranchById($id);
		$form = new branch_Form_FrmBranch();
		$this->view->frmBranch = $form->frmBranch($row);
	}		 
	public function deleteBranchAction(){
		$id = $this->getRequest()->getParam('id');
		$db = new users_Model_DbTable_DbUser();
		$db->deleteUser($id);
		Application_Form_FrmMessage::message("áž€áž¶áž›áž»áž”áž”áŸ’ážšáž—áŸ�áž‘áž¢áŸ’áž“áž€áž”áŸ’ážšáž¾áž”áŸ’ážšáž¶ážŸáŸ‹áž”áž¶áž“áž‡áŸ„áž‚áž‡áŸ�áž™");
		$this->_redirect('/users/');
	}
	public function getfillteraddAction(){
		if($this->getRequest()->IsPost()){
			$data = $this->getRequest()->getPost();
			$username = $data['username'];
			$sql= "SELECT username FROM tbwu_acl_user WHERE username = '$username'";
			$db = new Application_Model_DbTable_DbGlobal();
			$row = $db->getGlobalDbRow($sql);
			echo Zend_Json::encode($row);
			exit();
		}
	}
}

